package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();

        //去数据库查询改购物车是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//这一步已经将菜品的口味数据添加到shoppingCart对象中
        shoppingCart.setUserId(currentId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list == null || list.size()== 0){
            //如果数据库中不存在对应的购物车数据，判此次添加到购物车中的是菜品还是套餐，在去数据库中查询对应的数据，封装到对象中，再添加到购物车数据表中
            if(shoppingCartDTO.getDishId() != null){//此次添加的是菜品
                Dish byId = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(byId.getPrice());
            }else{//此次添加的是套餐
                Setmeal byId = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            }
            shoppingCartMapper.insert(shoppingCart);
        }else{//如果数据库中存在对应的数据，则修改对应的商品数量和金额(不用修改金额，数据表中的这个数据就是单价)
            shoppingCart = list.get(0);
            shoppingCart.setNumber(list.get(0).getNumber() + 1);
           shoppingCartMapper.updateNumberById(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> list = shoppingCartMapper.getByUserId(BaseContext.getCurrentId());
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 购物车中对应菜品或者套餐的数量减一
     * @param shoppingCartDTO
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //去数据库中查找对应的数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        shoppingCart = list.get(0);

        //如果该菜品或者套餐的数量为一，则删除   否则数量减一
        if(shoppingCart.getNumber() == 1){
            shoppingCartMapper.deleteOne(shoppingCart);
        }else{
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
    }
}
